package com.zzzzzz.oms.constractor;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;
import com.zzzzzz.core.datagrid.BaseData;
import com.zzzzzz.core.datagrid.BaseQueryForm;
import com.zzzzzz.utils.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzzzzz.oms.GCS;
import com.zzzzzz.plugins.shiro.ShiroUtils;
import com.zzzzzz.plugins.poi.ExportExcel;
import com.zzzzzz.plugins.poi.ImportExcel;

/**
 * @author hing
 * @version 1.0.0
 */
@Controller
public class ConstractorWeb {

	@Resource
	public ConstractorService constractorService;
	
	@Resource
	public ConstractorDao constractorDao;
	
	// 打开界面
	@RequestMapping(value = "/constractor/list", method = RequestMethod.GET)
	public String list() {
		return "oms/constractor";
	}

	//增加、修改数据
	@RequestMapping(value = "/constractor/save", method = RequestMethod.POST)
	@ResponseBody
	public BaseData save(@RequestBody @Valid Constractor constractor, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return new BaseData(GCS.ERRCD_PARAM);
		}
		constractorService.save(constractor, ShiroUtils.findUser());
		return new BaseData("0");
	}
	
	//停用数据
	@RequestMapping(value = "/constractor/updStByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStByIds(@RequestParam(value = "ids", required = true) List<Long> ids, @RequestParam(value = "st", required = true) Integer st) throws Exception {
		constractorDao.updStByIds(ids, st, ShiroUtils.findUser());
		return new BaseData("0");
	}	  
	
	//查找一条数据
	@RequestMapping(value = "/constractor/id/{id}", method = RequestMethod.GET)
	@ResponseBody 
	public BaseData findById(@PathVariable Long id) {
		Constractor constractor = constractorDao.findById(id);
		return new BaseData(constractor, null);
	}
	
	//动态查询数据
	@RequestMapping(value = "/constractor/findListBy", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findListBy(@RequestBody BaseQueryForm baseQueryForm) {
		Page page;
		List<Constractor> list;
		BaseData baseData;
		if(baseQueryForm.getPageNb() == null && baseQueryForm.getPageSize() == null){
			list = constractorDao.findListBy(null, baseQueryForm.getFfMap(), ShiroUtils.findUser());
			baseData = new BaseData(list, null);
		}else{
			page = baseQueryForm.getPage();
			list = constractorDao.findListBy(page, baseQueryForm.getFfMap(), ShiroUtils.findUser());
			baseData = new BaseData(list, page.getTotal());
		}
		return baseData;
	}

	//导出
	@RequestMapping(value = "/constractor/export")
	public String exportFileTemplate(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {				
			Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
			String fileName = "承运商数据.xlsx";
			List<Constractor> list = constractorDao.findListBy(null, params,ShiroUtils.findUser());
	    	new ExportExcel("承运商数据", Constractor.class, 1).setDataList(list).write(response, fileName).dispose();
	    	return null;
		 } catch (Exception e) {
				redirectAttributes.addFlashAttribute("msg", "承运商数据导出失败。");
				logger.warn("importFileTemplate", e);
		 }
		return "redirect:/msg";
	}
    
    private static final Logger logger = LoggerFactory.getLogger(ConstractorWeb.class);
}
